package com.sgl.blog.service.impl;

import com.sgl.blog.mapper.LikesMapper;
import com.sgl.blog.pojo.BlogLikes;
import com.sgl.blog.service.ArticleService;
import com.sgl.blog.service.LikesService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @标题： LikesService
 * @内容：
 * @作者： 宋志豪
 */
@Service
public class LikesServiceImpl implements LikesService {

    @Resource
    private LikesMapper likesMapper;

    @Resource
    private ArticleService articleService;

    @Override
    public boolean findOne(BlogLikes blogLikes) {
        return likesMapper.selectOne(blogLikes) != null;
    }

    @Override
    @Transactional
    public boolean onLike(BlogLikes blogLikes) {
        if (findOne(blogLikes)){
            articleService.likesCountSub(blogLikes.getArticleId());
            return !(likesMapper.delete(blogLikes) > 0);
        }
        articleService.likesCountPlus(blogLikes.getArticleId());
        return likesMapper.insert(blogLikes) > 0;
    }
}
